//
//  DestinationDatabaseManager.m
//  TravelToHelp
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationDatabaseManager.h"
#import <sqlite3.h>
#import "FMDB.h"

@interface DestinationDatabaseManager ()

@property (nonatomic, strong) FMDatabase * database;

@end

@implementation DestinationDatabaseManager

+ (instancetype)shareManager
{
    static DestinationDatabaseManager *manager;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        manager = [[DestinationDatabaseManager alloc] init];
        // 获取路径
        NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"DestinationDetail.sqlite"];
     
        // 初始化FMDB
        manager.database = [FMDatabase databaseWithPath:dbPath];
        [manager createTable];
    });
    return manager;
}

- (void)createTable
{
    [self.database open];
    [self.database executeUpdate:@"create table user (name text, recommended_reason text, cover text, descrip text, address text, arrival_type text, opening_time text, fee text)"];
   
    [self.database close];
}

- (void)insertDatabaseWithModel:(DestinationScenicModel *)model
{
    [self.database open];
    
    FMResultSet *rs =  [self.database executeQuery:@"insert into user(name,recommended_reason,cover,descrip,address,arrival_type,opening_time,fee) values(?,?,?,?,?,?,?,?)" withArgumentsInArray:@[model.name,model.recommended_reason,model.cover,model.descrip,model.address,model.arrival_type,model.opening_time,model.fee]];
    [rs next];
 
    [self.database close];
    
}

- (void)deleteByName:(NSString *)name
{
    [self.database open];
    [self.database executeUpdateWithFormat:@"delete from user where name = %@",name];
  
    [self.database close];
}

- (DestinationScenicModel *)selectModelByName:(NSString *)name
{
    [self.database open];
    
    NSString *Name = [self.database stringForQuery:@"select name from user where name = ?",name];
    NSString *recommended_reason = [self.database stringForQuery:@"select recommended_reason from user where name = ?",name];
    NSString *cover = [self.database stringForQuery:@"select cover from user where Name = ?",name];
    NSString *descrip = [self.database stringForQuery:@"select descrip from user where name = ?",name];
     NSString *address = [self.database stringForQuery:@"select address from user where name = ?",name];
    NSString *arrival_type = [self.database stringForQuery:@"select arrival_type from user where name = ?",name];
    NSString *opening_time = [self.database stringForQuery:@"select opening_time from user where name = ?",name];
     NSString *fee = [self.database stringForQuery:@"select fee from user where name = ?",name];
    DestinationScenicModel *model = [[DestinationScenicModel alloc] init];
    model.name = Name;
    model.recommended_reason = recommended_reason;
    model.cover = cover;
    model.descrip = descrip;
    model.address = address;
    model.arrival_type = arrival_type;
    model.opening_time = opening_time;
     model.fee = fee;
    [self.database close];
    return model;
}

- (NSMutableArray *)selectAllModels
{
    [self.database open];
    NSMutableArray *modelArray = [NSMutableArray array];
    //查询整个表
    FMResultSet * resultSet = [self.database executeQuery:@"select * from user"];
    
    //[resultSet next]遍历条件 遍历前为YES 遍历结束为NO
    while ([resultSet next] == YES)
    {
        //         用[resultSet stringForColumn:]或[resultSet intForColumn:]等方法把值转成Object-C的型态
        NSString * name = [resultSet stringForColumn:@"name"];
        NSString * recommended_reason = [resultSet stringForColumn:@"recommended_reason"];
        NSString * cover = [resultSet stringForColumn:@"cover"];
        NSString * descrip = [resultSet stringForColumn:@"descrip"];
        NSString * address = [resultSet stringForColumn:@"address"];
        NSString * arrival_type = [resultSet stringForColumn:@"arrival_type"];
        NSString * opening_time = [resultSet stringForColumn:@"opening_time"];
        NSString * fee = [resultSet stringForColumn:@"fee"];
        DestinationScenicModel *model = [[DestinationScenicModel alloc] init];
        model.name = name;
        model.recommended_reason = recommended_reason;
        model.cover = cover;
        model.descrip = descrip;
        model.address = address;
        model.arrival_type = arrival_type;
        model.opening_time = opening_time;
        model.fee = fee;

        [modelArray addObject:model];
    }
    [self.database close];
    return modelArray;
}




@end
